# -*- encoding: utf-8 -*-
'''
Created on Mar 30, 2012

@author: LONG HOANG GIANG
'''
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import datetime
from CrawlerLib.http import Http
import commonlib
from urllib import urlretrieve
import re
import simplejson as json

DOWNLOAD_PATH = '/tay-du-ky-hai/'

def download(url, path, filename):
    if (path[-1] != '/'): path += '/'
    downloadPath = path+filename
    print '----- download: {0} to {1}'.format(url, downloadPath)
    if not os.path.isdir(path):
        os.makedirs(path, 0777)
    a, b = urlretrieve(url, downloadPath)
    print a, b
    
def getGalleryPage(url):
    print 'getGalleryPage: {0}'.format(url)
    html = Http.getHtml(url)
    images = []
    chapterNum = commonlib.extractWithRegEx("tay-du-ky-hai-(\d+)", url, 1)
    if chapterNum == '': return
    tree = commonlib.buildTreeFromHTML(html)
    chapterTitle = commonlib.getElementText(tree.xpath("//div[@class='imageTitle']/a/h1"))
    print chapterTitle
    result = {'chapter': chapterTitle, 'images': []}
    
    for img in re.findall("http://anh\.24h\.com\.vn.+?tay-du-ky-hai.+\.jpg", html):
        if img not in images: images.append(img)
    
    i = 0
    for img in images:
        i += 1
        fileName = '{0}.jpg'.format(i)
        filePath = "{0}{1}/{2}.jpg".format(DOWNLOAD_PATH, chapterNum, fileName)
        download(img, '{0}{1}'.format(DOWNLOAD_PATH, chapterNum), fileName)
        result['images'].append(filePath)
    return result

def get(url):
    html = Http.getHtml(url)
    tree = commonlib.buildTreeFromHTML(html)
    
    if len(tree.xpath("//div[@class='imageContent_TrangAnh']")) < 1:
        print '---------------- NORMAL PAGE -----------------'
        return getNormalPage(url)
    else:
        print '---------------- GALLERY PAGE -----------------'
        return getGalleryPage(url)

def getNormalPage(url):
    print 'getNormalPage: {0}'.format(url)
    tree = Http.getXMLTree(url)
    chapterNum = commonlib.extractWithRegEx("tay-du-ky-hai-(\d+)", url, 1)
    if chapterNum == '': return
    
    chapterTitle = commonlib.getElementText(tree.xpath("//h1[contains(@class, 'baiviet-title')]"))
    result = {'chapter': chapterTitle, 'images': []}
    i = 0
    for imgNode in tree.xpath("//div[@class='baiviet-container']//img[contains(@src, 'tay-du-ky-hai')]"):
        i += 1
        img = imgNode.get('src')
        fileName = '{0}.jpg'.format(i)
        filePath = "{0}{1}/{2}".format(DOWNLOAD_PATH, chapterNum, fileName)
        download(img, '{0}{1}'.format(DOWNLOAD_PATH, chapterNum), fileName)
        result['images'].append(filePath)
    return result
        
def getListLink():
    currId = 0
    
    data = []
    
    for currId in range(0, 100, 10):
        url = 'http://hn.24h.com.vn/ajax/album_bai_cung_su_kien.php?divID=album_bai_cung_su_kien&title=&EventID=1479&currID=&type=TrangAnh&start={0}'.format(currId)
        tree = Http.getXMLTree(url)
        for item in tree.xpath("//li/a"):
            link = item.get('href')
            if link != '':
                link = commonlib.urlJoin(url, link)
                if 'tan-tay-du-ky-39-bach-cot-hot-girl' in link: continue
                data.append(get(link))
    
    jsondata = json.dumps(data)
    f = open('taydukyhai.out', 'w')
    f.write(jsondata)
    f.close()
    

if __name__ == '__main__':
    
    getListLink()
#    get('http://hn.24h.com.vn/cuoi-24h/tay-du-ky-hai-77-hang-gia-dai-nao-thien-cung-c70a345990.html')
    
    print '>> Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)